package demo.service.impl;

import demo.dao.UserMapper;
import demo.pojo.User;
import demo.pojo.UserExample;
import exception.TipException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author caosinuo
 * @date 2019/5/22 15:00
 */
@Service("userService")
public class UserService {


   @Autowired
   private UserMapper userDao;


   public User login(String phoneNum, String password) {
      if(StringUtils.isNotBlank(phoneNum) || StringUtils.isNotBlank(password)) {
         throw new TipException("用户名和密码为空");
      }

      UserExample example = new UserExample();
      UserExample.Criteria cirteria = example.createCriteria();
      cirteria.andPhoneNumEqualTo(phoneNum);
      long count = userDao.countByExample(example);
      if(count < 1) {
         return null;
      }
      List<User> user = userDao.selectByExample(example);
      if(user.size() == 0) {
         throw new TipException("用户名和密码错误");
      }
      return user.get(0);
   }
}
